package ru.acme.lab16.service.bank;

import ru.acme.lab16.domain.account.Account;
import ru.acme.lab16.domain.account.CheckingAccount;
import ru.acme.lab16.domain.bank.Bank;
import ru.acme.lab16.domain.client.Client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class BankReport {

	public static int getNbOfCustomers(Bank bank) {
		return bank.getClients().size();
	}

	public static int getNbOfAccounts(Bank bank) {
		int number = 0;
		for (Client client : bank.getClients()) {
			number += client.getAccounts().size();
		}
		return number;
	}

	public static List<Account> getAccountsSortedBySum(Bank bank) {
		List<Account> accounts = new ArrayList<Account>();
		for (Client client : bank.getClients()) {
			accounts.addAll(client.getAccounts());
		}
		Collections.sort(accounts);
		return accounts;
		
	}

	public static double getBankCredit(Bank bank) {
		double sumCredit = 0;
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts()) {
				if (account instanceof CheckingAccount) {
					sumCredit += ((CheckingAccount) account).getOverdraft();
				}
			}
		}
		return sumCredit;
	}

	public static Map<String, List<Account>> getCustomerAccount(Bank bank) {
		Map<String, List<Account>> map = new HashMap<String, List<Account>>();
		for (Client client : bank.getClients()) {
			List<Account> list = new LinkedList<Account>();
			list.addAll(client.getAccounts());
			map.put(client.getName(), list);
		}
		return map;
	}

}
